/*
 * Copyright @ 2024 weili23
 * com.sky.controller.admin 15:43
 * All right reserved.
 */

package com.sky.controller.admin;

import com.sky.constant.MessageConstant;
import com.sky.result.Result;
import com.sky.utils.AliOssUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.UUID;

/**
 * @desc: 公共controller
 * @author: weili23
 * @createTime: 2024/10/25 15:43
 * @version: v1.0
 */
@RestController
@Api(tags = "公用接口")
@Slf4j
@RequestMapping("/admin/common")
public class CommonController {
    @Resource
    private AliOssUtil aliOssUtil;

    @PostMapping("/upload")
    @ApiOperation("文件上传")
    public Result<String> upload(MultipartFile file){
        try {
            String originalFilename = file.getOriginalFilename();
            //截取文件后缀
            String fileExtension = originalFilename.substring(originalFilename.lastIndexOf("."));
            //存储文件名uuid+后缀
            String savefileName = UUID.randomUUID().toString()+fileExtension;
            String fileUrl = aliOssUtil.upload(file.getBytes(), savefileName);
            return Result.success(fileUrl);
        } catch (IOException e) {
            e.printStackTrace();
            log.error("文件上传失败：{}",e.getMessage());
        }
        return Result.error(MessageConstant.UPLOAD_FAILED);
    }
}
